package com.content.tinkhuyenmai;

import com.db.InfoProduct;
import com.db.Insert;
import com.db.MysqlConnect;
import com.img.Images;
import com.util.Common;
import com.util.Transition;
import com.util.VietDecode;

import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;

public class HtmlParser {

	
	public static void main(String[] args) {
		try {
			HtmlParser htmlParser = new HtmlParser();
			htmlParser.getUrlDetailProduct();
			//htmlParser.getInfoProduct("/Product.aspx?ID=13556","295");
			//htmlParser.urlAnalyse("http://tinkhuyenmai.net/Category.aspx");
//			for (int i = 1; i < 100; i++) { 
//				htmlParser.urlAnalyse("http://tinkhuyenmai.net/Category.aspx?Page="+i);
//			}
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
	}
	
	
//	public void urlAnalyse(String mainUrl){
//		try {
//			
//			Source source = null;
//			source=new Source(new URL(mainUrl));
//			source.fullSequentialParse();
//			
//			List<Element> linkElements=source.getAllElements(HTMLElementName.A);
//			for (Element linkElement : linkElements) {
//				String href=linkElement.getAttributeValue("href");
//				if (href==null) continue;
//				//danh sach link chi tiet san pham
//				if(href.indexOf("Product.aspx?ID=")==0){
//					System.out.println(href);
//					getInfoProduct("/"+href);
//				}					
//			}
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//			System.out.println(e.toString());
//		}
//	}
		
	/*
	 * get info product in suburl va cap nhat vao db
	 */
	public void getInfoProduct(String subUrl,String idTkm){		
		try {			
			String sourceUrl="";
			Source source = null;
			Process process = new Process();
			
			sourceUrl = process.ROOT_URL + subUrl;
			//sourceUrl = process.URL;			
			source=new Source(new URL(sourceUrl));
			source.fullSequentialParse();
			
			//set info product
			process.setProductID(subUrl);
			process.setProductName(source);	
			process.setProductCode(source);
			process.setSummary(source);
			process.setDomain(source);
			
			process.setProductType(source);
			process.setProductContent(source);
			process.setPrice(source);						
			process.setPriceSale(source);
			process.setDateSaleBegin(source);
			process.setDateSaleEnd(source);
			
			process.setYm(source);
			process.setEmail(source);
			process.setContactPeople(source);
			process.setContactMobile(source);
			process.setContactPhone(source);
			process.setContactAddress(source);
			process.setImg(source);
			
			String imgName="";
			if(process.getImg()!=null){
				Images img = new Images();
				img.setImgUrl(process.getImg());
				imgName = img.saveImage();
			}
			
			
			//get info product
			InfoProduct inf = new InfoProduct();
			//System.out.println(process.getDomain());
			Transition t = new Transition();
			String domain = process.getDomain();
			
			VietDecode vd = new VietDecode();			
			String code =vd.convert(domain).replace(" ", "").toLowerCase() ;	
			if(code.equals("hochiminh")) code="tphcm";
			String cityId = t.getCity(code);
			
			String catId = t.getCatId(idTkm);
			System.out.println(catId);
			inf.SourceId = process.getProductId();
			inf.SourceUrl = sourceUrl;
			inf.Name = process.getProductName();			
			inf.Alias = t.convertAlias(inf.Name);
			inf.Keyword = t.keywordAnalyse(inf);
			inf.Code = process.getProductCode();
			inf.Type = process.getProductType();
			inf.Content = process.getProductContent();
			inf.Price = process.getPrice();
			inf.PriceSale = process.getPriceSale();
			inf.BeginDateSale =process.getDateSaleBegin();
			inf.EndDateSale = process.getDateSaleEnd();
			inf.Ym = process.getYm();
			inf.Email = process.getEmail();
			inf.People = process.getContactPeople();
			inf.Mobile = process.getContactMobile();
			inf.Phone = process.getContactPhone();
			inf.Address = process.getContactAddress();
			inf.Summary = process.getSummary();
			inf.City = cityId;
			inf.CatId = catId;
			inf.Img = imgName;		
			
						
			
//			System.out.println("Name: "+inf.Name);
//			System.out.println("Code: "+inf.Code);
//			System.out.println("Type: "+inf.Type);
//			System.out.println("Conent: "+inf.Content);
//			System.out.println("Price: "+inf.Price);
//			System.out.println("Price Sale: "+inf.PriceSale);
//			System.out.println("Date begin: "+inf.BeginDateSale);
//			System.out.println("Date end: "+inf.EndDateSale);
//			System.out.println("YM: "+inf.Ym);
//			System.out.println("Email: "+inf.Email);
//			System.out.println("People: "+inf.People);
//			System.out.println("Mobile: "+inf.Mobile);
//			System.out.println("Phone: "+inf.Phone);
//			System.out.println("Address: "+inf.Address);
//			System.out.println("Img: "+inf.Img);
//			System.out.println("Summary: "+inf.Summary);
//			System.out.println("Domain: "+inf.City);
			
			
			Insert ins = new Insert();
			String result = null;
			result = ins.insert(inf);
			if(result.equals("S001")){
				System.out.println("Success!"+ inf.Name);
			}else{
				System.out.println("Error: "+result);
			}
			
		} catch (Exception e) {
			System.out.println(e.toString());			
		}
	}
	
	/**
	 * lay toan bo url detail product theo url cat category
	 */
	public void getUrlDetailProduct(){
		try {
			
			//String result = "E001|Co loi phat sinh";
			PreparedStatement pstmt = null;
		    Connection conn  = null;
		    MysqlConnect mysql = new MysqlConnect();		    
		    
		    try {
		    	conn = mysql.getConnection();
			    String vc_sql="SELECT id,url,id_tkm FROM  `product_cat`";
			    String catUrl=null;
			    pstmt = conn.prepareStatement(vc_sql);			    
			    ResultSet rs = pstmt.executeQuery();
			    String href=null;
				while (rs.next()){
					catUrl= rs.getString("url");
					List<Element> link = new Common().getAllUrl(catUrl);
					for (Element item : link) {
						href=item.getAttributeValue("href");
						if (href==null) continue;
						//danh sach link chi tiet san pham
						if(href.indexOf("Product.aspx?ID=")==0){
							System.out.println(href);
							getInfoProduct("/"+href,rs.getString("id_tkm"));
						}						
					}
				}
			    
			} catch (Exception e) {
				// TODO: handle exception
			}finally {
		    	pstmt.close();
		    	conn.close();
		    }
			
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
	}
//	
//	//public void 
//	
//	public void getAllUrlDetailProduct(String catUrl){
//		try {
//			
//			Source source = null;
//			source=new Source(new URL(catUrl));
//			source.fullSequentialParse();
//			
//			List<Element> linkElements=source.getAllElements(HTMLElementName.A);
//			for (Element linkElement : linkElements) {
//				String href=linkElement.getAttributeValue("href");
//				if (href==null) continue;
//				//danh sach link chi tiet san pham
//				if(href.indexOf("Product.aspx?ID=")==0){
//					System.out.println(href);
//					getInfoProduct("/"+href);
//				}					
//			}
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//			System.out.println(e.toString());
//		}
//	}
	
}
